package com.mcl.printer.widgets;


import com.inter.OnShakeListener;

public class SkakeFilter {

    private int filterCount;
    private int recordFilterCount = 0;
    private boolean isCheckShake = true;
    private boolean isRepeat = false;
    private OnShakeListener onShakeListener;

    public SkakeFilter(int filterCount, OnShakeListener onShakeListener) {
        this.filterCount = filterCount;
        this.onShakeListener = onShakeListener;
    }

    public void shake(boolean isShake) {
        if (isShake) {
            if (isCheckShake) {
                recordFilterCount++;
                if (recordFilterCount >= filterCount) {
                    isRepeat = false;
                    isCheckShake = false;
                    if (onShakeListener != null) {
                        onShakeListener.onShake();
                    }
                }
            }
        } else {
            recordFilterCount = 0;
            isCheckShake = true;
            if (onShakeListener != null) {
                if (!isRepeat) {
                    isRepeat = true;
                    onShakeListener.onRecover();
                }
            }
        }
    }
}
